﻿using System;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using Stimulus360.Controllers.Home;
using System.Runtime.Serialization.Json;
using Stimulus360.Models.Data.CustomTypes;
using System.Windows.Controls.DataVisualization.Charting;

namespace HomeViewGraphs
{
    /// <summary>
    ///     Page represents the graph data to be shown for the Home page
    /// </summary>
    public partial class Graphs 
    {
        /// <summary>
        ///     Default constructor. Hooks the PageLoaded event
        /// </summary>
        public Graphs()
        {
            InitializeComponent();
            Loaded += PageLoaded;
        }

        /// <summary>
        ///     Property to get the parent page uri
        /// </summary>
        private static string Host
        {
            get
            {
                return System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();
            }
        }

        /// <summary>
        ///     Page Loaded event handler. Calls the Host application to get start reading the graph data
        /// </summary>
        /// <param name="sender">
        ///     The Page itself
        /// </param>
        /// <param name="e">
        ///     Routed event args
        /// </param>
        private void PageLoaded(object sender, RoutedEventArgs e)
        {
            WebClient mvc = new WebClient();
            mvc.OpenReadCompleted += OpenReadCompleted;
            string hostString = Host + "/GraphData";
            mvc.OpenReadAsync(new Uri(hostString));
        }

        /// <summary>
        ///     Method called when the graph data is returned from the host application
        /// </summary>
        /// <param name="sender">
        ///     The page itself
        /// </param>
        /// <param name="e">
        ///     The args containing the result graph data.
        /// </param>
        private void OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(GraphData));
            GraphData model = (GraphData)json.ReadObject(e.Result);
            ICollection<BudgetSpendSummary> investmentTrend = model.BudgetSummary;
            BudgetSummary.DataContext = investmentTrend;
            BudgetSummary.Title = "    Budget/Spend Summary" + Environment.NewLine + "Top 5 Investment Categories";
            BudgetSummary.Series.Add(new ColumnSeries { DataContext = investmentTrend, ItemsSource = investmentTrend, DataPointStyle = (Style)Resources["ToolTipStyle"], DependentValuePath = "Budget", IndependentValuePath = "InvestmentCategorySubstring", Title = "Budget" });
            BudgetSummary.Series.Add(new ColumnSeries { ItemsSource = investmentTrend, DependentValuePath = "Spent", DataPointStyle = (Style)Resources["SecondToolTipStyle"], IndependentValuePath = "InvestmentCategorySubstring", Title = "Spent" });

            NewJobsChart.DataContext = investmentTrend;
            NewJobsChart.Title = "             New Jobs" + Environment.NewLine + "Top 5 Investment Categories";
            NewJobsChart.Series.Add(new ColumnSeries { ItemsSource = investmentTrend, IndependentValuePath = "InvestmentCategorySubstring", DataPointStyle = (Style)Resources["ToolTipStyle"], DependentValuePath = "JobCount", Title = "" });

            ICollection<IdeaTrend> ideaTrend = model.IdeaTrend;
            IdeationChart.DataContext = ideaTrend;
            IdeationChart.Series.Add(new LineSeries { ItemsSource = ideaTrend, IndependentValuePath = "Quarter", DependentValuePath = "IdeaCount" });
            ICollection<ProjectTrend> projTrend = model.ProjectTrend;
            ProjTrend.DataContext = projTrend;
            ProjTrend.Series.Add(new LineSeries { ItemsSource = projTrend, IndependentValuePath = "Quarter", DependentValuePath = "ProjectsCompleted", Title = "Completed" });
            ProjTrend.Series.Add(new LineSeries { ItemsSource = projTrend, IndependentValuePath = "Quarter", DependentValuePath = "InProgress", Title = "In Progress" });
            ProjTrend.Series.Add(new LineSeries { ItemsSource = projTrend, IndependentValuePath = "Quarter", DependentValuePath = "StalledProjects", Title = "Stalled" });
        }
    }
}
